package edu.thu.studio.ormdesigner.common;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import edu.thu.studio.ormdesigner.model.ORMModel;

public class ListenerAppender {
    
    public static void addTextListener(final Text text, final AbstractDialog dialog, boolean imeOn) {
        addFocusListener(text, imeOn);
        if (dialog != null) {
            addModifyListener(text, dialog);
        }
    }
    
    public static void addTextAreaListener(final Text text, final AbstractDialog dialog, boolean selectAll,
        boolean imeOn) {
        
    }
    
    public static void addFocusListener(final Text text, final boolean imeOn) {
        addFocusListener(text, true, imeOn);
    }
    
    public static void addFocusListener(final Text text, final boolean selectAll, final boolean imeOn) {
        text.addFocusListener(new FocusAdapter() {
            
            /**
             * {@inheritDoc}
             */
            @Override
            public void focusGained(FocusEvent e) {
                ORMModel ormModel = (ORMModel) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                    .getActiveEditor().getAdapter(ORMModel.class);
                
                if (ormModel != null) {
                    if (imeOn) {
                        text.getShell().setImeInputMode(SWT.DBCS | SWT.NATIVE);
                        
                    } else {
                        text.getShell().setImeInputMode(SWT.ALPHA);
                    }
                }
                
                if (selectAll) {
                    text.selectAll();
                }
                
                super.focusGained(e);
            }
            
            /**
             * {@inheritDoc}
             */
            @Override
            public void focusLost(FocusEvent e) {
                // text.clearSelection();
                super.focusLost(e);
            }
        });
    }
    
    public static void addModifyListener(final Text text, final AbstractDialog dialog) {
        text.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                dialog.validate();
            }
            
        });
    }
    
    public static void addComboListener(final Combo combo, final AbstractDialog dialog, final boolean imeOn) {
        addFocusListener(combo, imeOn);
        
        if (dialog != null) {
            combo.addModifyListener(new ModifyListener() {
                public void modifyText(ModifyEvent e) {
                    dialog.validate();
                }
            });
        }
    }
    
    public static void addFocusListener(final Combo combo, final boolean imeOn) {
        combo.addFocusListener(new FocusAdapter() {
            
            /**
             * {@inheritDoc}
             */
            @Override
            public void focusGained(FocusEvent e) {
                ORMModel ormModel = (ORMModel) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                    .getActiveEditor().getAdapter(ORMModel.class);
                
                if (ormModel != null) {
                    if (imeOn) {
                        combo.getShell().setImeInputMode(SWT.DBCS | SWT.NATIVE);
                        
                    } else {
                        combo.getShell().setImeInputMode(SWT.ALPHA);
                    }
                }
                
                super.focusGained(e);
            }
            
            /**
             * {@inheritDoc}
             */
            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e);
            }
        });
    }
    
    public static void addTabListener(final TabFolder tabFolder,
			final List<ValidatableTabWrapper> tabWrapperList) {
		tabFolder.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
			}

			public void widgetSelected(SelectionEvent e) {
				int index = tabFolder.getSelectionIndex();

				ValidatableTabWrapper selectedTabWrapper = tabWrapperList
						.get(index);
				selectedTabWrapper.restruct();
				selectedTabWrapper.setInitFocus();
			}

		});
	}
    
}
